package com.mall.pay.services;

import com.alipay.demo.trade.Main;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.order.OrderUpdateService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.constant.OrderStatusConstants;
import com.mall.order.dto.OrderUpdateResponse;
import com.mall.order.dto.OrderUpdateReuqest;
import com.mall.pay.PaymentService;
import com.mall.pay.constant.PayRetCode;
import com.mall.pay.converter.PaymentConverter;
import com.mall.pay.dal.entitys.Payment;
import com.mall.pay.dal.persistence.PaymentMapper;
import com.mall.pay.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author xudengfeng
 * @date 2020/12/21 20:28
 */

@Service(interfaceClass = PaymentService.class)
@Component
public class PayServiceImpl implements PaymentService {

    @Autowired
    PaymentMapper paymentMapper;

    @Autowired
    PaymentConverter paymentConverter;

    @Reference(timeout = 3000, check = false)
    OrderUpdateService orderUpdateService;

    @Autowired
    Main main;

    @Value("${webpath}")
    String webpath;

    // 测试方法
    @Override
    public PaymentResponse queryById(PaymentRequest paymentRequest) {

        PaymentResponse resp = new PaymentResponse();

        try {
            paymentRequest.requestCheck();
            Integer id = paymentRequest.getId();
            Payment payment = paymentMapper.selectByPrimaryKey(id);
            PaymentDto paymentDto = paymentConverter.payment2Dto(payment);
            resp.setPaymentDto(paymentDto);
        } catch (Exception e) {

        }
        return resp;
    }


    @Override
    public PayResponse getAliPayQrCode(PayRequest payRequest) {
        PayResponse resp = new PayResponse();
        // 获取二维码图片,并保存到本地
        try {
            payRequest.requestCheck();
            // 返回流水单号 之后通过此号码查询订单状态
            String path = main.test_trade_precreate(payRequest.getMap());
            if (!"-1".equals(path)) {
                path = webpath + path;
            }
            resp.setPicUrl(path);
            resp.setCode(PayRetCode.SUCCESS.getCode());
            resp.setMsg(PayRetCode.SUCCESS.getMessage());
        } catch (ValidateException validateException) {
            resp.setCode(PayRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode());
            resp.setMsg(PayRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        } catch (Exception e) {
            resp.setCode(PayRetCode.SYSTEM_ERROR.getCode());
            resp.setMsg(PayRetCode.SYSTEM_ERROR.getMessage());
        }
        return resp;
    }

    @Override
    public PaymentStatusResponse queryPaymentStatusByOrderId(PaymentStatusResquest paymentStatusResquest) {

        PaymentStatusResponse resp = new PaymentStatusResponse();

        String orderId = paymentStatusResquest.getOrderId();
        String status = main.test_trade_query(orderId);

        if (PayRetCode.SUCCESS_PAY.getCode().equals(status)) {

            // 支付成功,需要修改订单状态
            OrderUpdateReuqest orderUpdateReuqest = new OrderUpdateReuqest();
            orderUpdateReuqest.setOrderId(orderId);
            orderUpdateReuqest.setStatus(OrderStatusConstants.ORDER_STATUS_PAYED);

            OrderUpdateResponse orderUpdateResponse = orderUpdateService.updateStatusByOrderId(orderUpdateReuqest);

            // 对修改结果进行判断
            String code = orderUpdateResponse.getCode();
            String msg = orderUpdateResponse.getMsg();
            if (OrderRetCode.SUCCESS.getCode().equals(code)) {
                // 修改订单状态成功
                resp.setMsg(PayRetCode.SUCCESS_PAY.getMessage());
                resp.setCode(status);
            }
            resp.setMsg(msg);
        } else {
            resp.setMsg(status);
        }
        return resp;
    }
}
